/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.txt.access.capability.demo.service;

import it.txt.access.capability.commons.signer.model.X509DocumentSignerInfo;
import it.txt.access.capability.demo.service.impl.CapabilityAuthDemoServiceImpl;
import java.io.FileInputStream;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
//import org.apache.html.
/**
 *
 * @author seccia
 */
public class CapabilityAuthDemoServiceActivator implements BundleActivator {

    private static final Logger LOGGER = Logger.getLogger(CapabilityAuthDemoServiceActivator.class.getName());
    public static final String CAP_SERVICE_ID = "it.txt.access.capability.demo.service.service_id";
    public static final String CAP_SCHEMAS_DIR = "it.txt.access.capability.demo.service.schemas_dir";
    public static final String CAP_KEYSTORE_PWD = "it.txt.access.capability.demo.service.keystore_pwd";
    public static final String CAP_KEYSTORE_PATH = "it.txt.access.capability.demo.service.keystore_path";
    public static final String CAP_PRIVATE_KEY_PWD = "it.txt.access.capability.demo.service.private_key_pwd";
    private ICapabilityAuthDemoService capabilityAuthService;
    private static ClassLoader contextClassLoader;
    private ServiceRegistration registration;
    


    public void start(BundleContext context) throws Exception {
        long id = context.getBundle().getBundleId();
        LOGGER.log(Level.INFO, "[TXTAccess :: Capability :: Applications "
                + ":: Demos :: OSGi :: Service]: Bundle ID: {0}", id);
        setContextClassLoader(CapabilityAuthDemoServiceActivator.class.getClassLoader());
        //File configurationProperties = context.getDataFile(configFileProp);
        Properties configProperties = new Properties();
        configProperties.load(new FileInputStream("conf/config.properties"));
        String capabilitySchemasPath = configProperties.getProperty(CAP_SCHEMAS_DIR);
        X509DocumentSignerInfo signerInfo = new X509DocumentSignerInfo();
        signerInfo.setSignerID(configProperties.getProperty(CAP_SERVICE_ID));
        signerInfo.setKeystorePath(configProperties.getProperty(CAP_KEYSTORE_PATH));
        signerInfo.setKeystorePwd(configProperties.getProperty(CAP_KEYSTORE_PWD).toCharArray());
        signerInfo.setPrivateKeyPwd(configProperties.getProperty(CAP_PRIVATE_KEY_PWD).toCharArray());
        capabilityAuthService = new CapabilityAuthDemoServiceImpl(capabilitySchemasPath, signerInfo);
        registration = context.registerService(ICapabilityAuthDemoService.class.getName(),
                capabilityAuthService, getRuleServiceProperties());
        LOGGER.log(Level.INFO, "[TXTAccess-CAS]: Registered Service: {0}",
                registration.getReference().toString());
    }

    public void stop(BundleContext context) throws Exception {
        registration.unregister();
    }

    @SuppressWarnings("all")
    private Dictionary<String, Object> getRuleServiceProperties() {
        Dictionary<String, Object> result = new Hashtable<String, Object>();
        result.put("Capability_Schema_Version", "3.3");
        return result;
    }

    public static ClassLoader getContextClassLoader() {
        return contextClassLoader;
    }

    public static void setContextClassLoader(ClassLoader contextClassLoader) {
        CapabilityAuthDemoServiceActivator.contextClassLoader = contextClassLoader;
    }
}
